﻿@typeparam TItem
@inherits TableBase<TItem>

<div class="bootstrap-table">
    @if (ShowToolBar)
    {
        <div class="fixed-table-toolbar">
            <div class="float-left bs-bars">
                <TableToolbar>
                    @if (ShowDefaultButtons)
                    {
                        <TableToolbarButton class="btn btn-success" Icon="fa fa-plus" Title="新增" OnClick="Add" />
                        <TableToolbarButton class="btn btn-danger" Icon="fa fa-remove" Title="删除" OnClick="Delete" />
                        <TableToolbarButton class="btn btn-primary" Icon="fa fa-pencil" Title="编辑" OnClick="Edit" />
                    }
                    @TableToolbarTemplate
                </TableToolbar>
            </div>
            <div class="float-right columns columns-right">
                @if (ShowRefresh)
                {
                    <button class="btn btn-secondary" type="button" title="刷新" @onclick="Query">
                        <i class="fa fa-refresh"></i><span>刷新</span>
                    </button>
                }
            </div>
            @if (ShowSearch)
            {
                <div class="float-right search btn-group">
                    <div class="input-group">
                        <input class="form-control search-input" type="text" placeholder="搜索" @bind="SearchText" />
                        <div class="input-group-append">
                            <button class="btn btn-secondary" type="button" title="搜索" @onclick="SearchClick">
                                <i class="fa fa-search"></i><span>搜索</span>
                            </button>
                            <button class="btn btn-secondary" type="button" title="清空过滤" @onclick="ClearSearchClick">
                                <i class="fa fa-trash"></i> <span>清空过滤</span>
                            </button>
                            @if (ShowAdvancedSearch)
                            {
                                <button class="btn btn-secondary" type="button" title="高级搜索" @onclick="AdvancedSearchClick">
                                    <i class="fa fa-search-plus"></i><span>高级搜索</span>
                                </button>
                            }
                        </div>
                    </div>
                </div>
            }
        </div>
    }
    <div class="@(FixedHeader ? "table-wrapper table-fixed-header table-fixed" : "table-wrapper")">
        @if (FixedHeader)
        {
            <div class="fixed-table-header">
                <table class="table table-bordered table-hover table-selected">
                    <thead>
                        <tr>
                            @if (ShowCheckbox)
                            {
                                <th class="table-col-checkbox"><Checkbox TItem="TItem" SetCheckCallback="CheckState" OnClick="ToggleCheck"></Checkbox></th>
                            }
                            @if (ShowLineNo)
                            {
                                <th class="table-col-lineno">行号</th>
                            }
                            <TableHeader OnSort="OnSort">
                                @TableHeader?.Invoke(EditModel)
                            </TableHeader>
                            @if (ShowExtendButtons)
                            {
                                <th>@ButtonTemplateHeaderText</th>
                            }
                        </tr>
                    </thead>
                </table>
            </div>
            <div class="fixed-table-body invisible">
                <table class="table table-bordered table-hover table-selected" id="@RetrieveId()">
                    <thead>
                        <tr>
                            @if (ShowCheckbox)
                            {
                                <th class="table-col-checkbox"><Checkbox TItem="TItem" SetCheckCallback="CheckState" OnClick="ToggleCheck"></Checkbox></th>
                            }
                            @if (ShowLineNo)
                            {
                                <th class="table-col-lineno">行号</th>
                            }
                            <TableHeader OnSort="OnSort">
                                @TableHeader?.Invoke(EditModel)
                            </TableHeader>
                            @if (ShowExtendButtons)
                            {
                                <th>@ButtonTemplateHeaderText</th>
                            }
                        </tr>
                    </thead>
                    <tbody>
                        @for (int index = 0; index < Items.Count(); index++)
                        {
                            <tr>
                                @if (ShowCheckbox)
                                {
                                    <td class="table-col-checkbox"><Checkbox TItem="TItem" Item="Items.ElementAt(index)" SetCheckCallback="item => SelectedItems.Contains(item) ? CheckBoxState.Checked : CheckBoxState.UnChecked" OnClick="ToggleCheck"></Checkbox></td>
                                }
                                @if (ShowLineNo)
                                {
                                    <td class="table-col-lineno">@(index + 1 + (PageIndex - 1) * PageItems)</td>
                                }
                                @RowTemplate?.Invoke(Items.ElementAt(index))
                                @if (ShowExtendButtons)
                                {
                                    <td>@ButtonTemplate?.Invoke(Items.ElementAt(index))</td>
                                }
                            </tr>
                        }
                    </tbody>
                    <tfoot>
                        <tr>@TableFooter</tr>
                    </tfoot>
                </table>
            </div>
        }
        else
        {
            <table class="table table-striped table-bordered table-hover table-selected" id="@($"{Id}_table")">
                <thead>
                    <tr>
                        @if (ShowCheckbox)
                        {
                            <th class="table-col-checkbox"><Checkbox TItem="TItem" SetCheckCallback="CheckState" OnClick="ToggleCheck"></Checkbox></th>
                        }
                        @if (ShowLineNo)
                        {
                            <th class="table-col-lineno">行号</th>
                        }
                        <TableHeader OnSort="OnSort">
                            @TableHeader?.Invoke(EditModel)
                        </TableHeader>
                        @if (ShowExtendButtons)
                        {
                            <th>@ButtonTemplateHeaderText</th>
                        }
                    </tr>
                </thead>
                <tbody>
                    @for (int index = 0; index < Items.Count(); index++)
                    {
                        <tr>
                            @if (ShowCheckbox)
                            {
                                <td class="table-col-checkbox"><Checkbox TItem="TItem" Item="Items.ElementAt(index)" SetCheckCallback="item => SelectedItems.Contains(item) ? CheckBoxState.Checked : CheckBoxState.UnChecked" OnClick="ToggleCheck"></Checkbox></td>
                            }
                            @if (ShowLineNo)
                            {
                                <td class="table-col-lineno">@(index + 1 + (PageIndex - 1) * PageItems)</td>
                            }
                            @RowTemplate?.Invoke(Items.ElementAt(index))
                            @if (ShowExtendButtons)
                            {
                                <td>@ButtonTemplate?.Invoke(Items.ElementAt(index))</td>
                            }
                        </tr>
                    }
                </tbody>
                @if (ShowFooter)
                {
                    <tfoot>
                        <tr>@TableFooter</tr>
                    </tfoot>
                }
            </table>
        }
    </div>
    @if (ShowPagination)
    {
        <Pagination PageItems="PageItems" TotalCount="TotalCount" PageIndex="PageIndex" OnPageClick="PageClick" OnPageItemsChange="PageItemsChange"></Pagination>
    }
</div>

@if (OnDelete != null)
{
    <Modal @ref="ConfirmModal" Id="@($"{Id}_confirm")" Title="数据删除">
        <ModalBody>
            <div class="modal-confirm-body">您确定要删除选中的所有数据吗？</div>
        </ModalBody>
        <ModalFooter>
            <button type="button" class="btn btn-danger" @onclick="Confirm">
                <i class="fa fa-trash-o"></i>
                <span>我要删除</span>
            </button>
            <button type="button" class="btn btn-secondary" data-dismiss="modal">
                <i class="fa fa-times"></i>
                <span>取消</span>
            </button>
        </ModalFooter>
    </Modal>
}

@if (OnSave != null || OnAdd != null)
{
    <SubmitModal @ref="EditModal" Id="@($"{Id}_edit")" TItem="TItem" Title="@SubmitModalTitle" Size="ModalSize.ExtraLarge" @bind-Model="EditModel" OnValidSubmit="Save">
        <ModalBody>
            @EditTemplate?.Invoke(EditModel)
        </ModalBody>
    </SubmitModal>
}

@if (ShowSearch && ShowAdvancedSearch)
{
    <Modal @ref="SearchModal" Id="@($"{Id}_search")" Title="查询条件">
        <ModalBody>
            <LgbEditForm class="form-inline" Id="@Id" Model="QueryModel">
                <div class="row">
                    @SearchTemplate?.Invoke(QueryModel)
                </div>
            </LgbEditForm>
        </ModalBody>
        <ModalFooter>
            <button type="button" class="btn btn-info" data-dismiss="modal" @onclick="ResetSearchClick">
                <i class="fa fa-trash-o"></i>
                <span>重置</span>
            </button>
            <button type="button" class="btn btn-primary" data-dismiss="modal" @onclick="SearchClick">
                <i class="fa fa-search"></i>
                <span>查询</span>
            </button>
        </ModalFooter>
    </Modal>
}
